package sy.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.SysDivisionDaoI;
import sy.model.SysDivision;

@Repository("sysDivisionDao")
public class SysDivisionDaoImpl implements SysDivisionDaoI {
	
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}
	
	@Override
	public Serializable save(SysDivision sysDivision) {
		return getCurrentSession().save(sysDivision);//直接用
		
	}

	@Override
	public void remove(SysDivision sysDivision) {
		// TODO Auto-generated method stub
		getCurrentSession().delete(sysDivision);
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysDivisionDaoI#update(sy.model.SysDivision)
	 */
	@Override
	public void update(SysDivision sysDivision) {
		// TODO Auto-generated method stub
		getCurrentSession().update(sysDivision);
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysDivisionDaoI#list()
	 */
	@Override
	public List<SysDivision> list() {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery("from SysDivision").list();
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysDivisionDaoI#getSysDivisionById(java.lang.String)
	 */
	@Override
	public SysDivision getSysDivisionById(String id) {
		// TODO Auto-generated method stub
		String hql = "from SysDivision where id = :id";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("id", id);
		List<SysDivision> list = query.list();
		if (list.size()>0){
			return list.get(0);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysDivisionDaoI#listSysDivisionByKey(java.lang.String)
	 */
	@Override
	public List<SysDivision> listSysDivisionByKey(String key) {
		// TODO Auto-generated method stub
		String hql = "from SysDivision where code like :key or name like :key or sysDivision.id like :key";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("key", "%"+key+"%");
		List<SysDivision> list = query.list();
		return list;
	}

}
